package com.md.mainpage.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.md.livingwidget.navigation.PHNavigationBar
import com.md.mainpage.R
import com.md.mainpage.adapter.MainCategoryLeftAdapter
import com.md.mainpage.adapter.MainCategoryRightAdapter
import com.md.mainpage.databinding.ActivityCategoriesBinding
import com.md.mainpage.interfaces.MainCategoriesContract
import com.md.mainpage.presenter.MainCategoryPresenter
import com.md.network.api.Category

class MainCategoryActivity :AppCompatActivity(),PHNavigationBar.OnBarClickListener, MainCategoriesContract.View , MainCategoryLeftAdapter.SelectedCategoryCallback {
    private lateinit var binding:ActivityCategoriesBinding
    private val tagList= ArrayList<String>()
    private lateinit var recCategories:RecyclerView
    private lateinit var recRightCategory:RecyclerView
    private lateinit var adapterLeft:MainCategoryLeftAdapter
    private lateinit var adapterRight:MainCategoryRightAdapter
    private lateinit var mPresenter:MainCategoryPresenter


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCategoriesBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
        initNav()
        initView()
        requestData()

    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

    private fun initNav(){
        binding.navBar.centerTvTitle.apply {
            text=resources.getString(R.string.main_category)
        }
        binding.navBar.setOnBarClickListener(this)
    }

    private fun initView(){
        recCategories= binding.recCategory
        adapterLeft = MainCategoryLeftAdapter(this,ArrayList(),this)
        recCategories.apply {
            layoutManager = LinearLayoutManager(this@MainCategoryActivity)
            adapter = adapterLeft
        }
        recRightCategory= binding.recRightCategory
        adapterRight = MainCategoryRightAdapter(this,ArrayList())
        recRightCategory.apply {
            layoutManager = LinearLayoutManager(this@MainCategoryActivity)
            adapter = adapterRight
        }



    }

    private fun requestData(){
        mPresenter = MainCategoryPresenter()
        mPresenter.attachView(this)
        mPresenter.getCategoriesList()
    }



    override fun onBarLeft1Back(v: View?) {
        finish()
    }

    override fun onBarRight2Btn(v: View?) {
        //do nothing
    }

    override fun onBarRight1Btn(v: View?) {
    }

    override fun onBarRight3Btn(v: View?) {
        //do nothing
    }

    override fun onBarLeft2Back(v: View?) {
        //do nothing
    }

    override fun onBarRight1Tv(v: View?) {
        //do nothing
    }

    override fun showCategoriesList(categories: List<Category>) {
        if (categories.isNotEmpty()) categories[0].isSel=true
        adapterLeft.setData(categories)
        selectedCategory(categories[0])

    }

    override fun showSingleCategory(category: Category) {
        adapterRight.setData(category.groups)
    }

    override fun showError(errorMsg: String) {

    }
    /**
     * @param category 第一次网络请求，就是mPresenter.getCategoriesList获取到数据中的category，
     * 其属性groups为null， 坤阳说担心数据量太大云端、客户端都可能会出bug。所以这里又做了一次网络请求
     * 根据id获取其详细值。
     */
    override fun selectedCategory(category: Category) {
        var cId= category.id.toInt()
        mPresenter.getCategoryInfo(cId)
    }


}